package cz.cvut.manatee.client.concordance;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;

import cz.cvut.manatee.client.Locator;
import cz.cvut.manatee.client.RemoteServiceFrontendAsync;
import cz.cvut.manatee.shared.CorpusInformations;
import cz.cvut.manatee.shared.concordance.CollectedSearchParameters;
import cz.cvut.manatee.shared.concordance.CollectedSimpleOrCqlParameters;
import cz.cvut.manatee.shared.concordance.ResultTableRow;

public class ConcordanceWidget extends Composite {

	private final SearchWidget searchForm;
	private final ResultsTableWigdet resultTable;
	private VerticalPanel main;
	
	RemoteServiceFrontendAsync service;
	
	
	public ConcordanceWidget(){
		
		service = Locator.getInstance();
		
		searchForm = new SearchWidget();
		
		resultTable = new ResultsTableWigdet();		
		
		main = new VerticalPanel();
		main.add(searchForm);
		main.add(resultTable);

		setRemoteCorpuses();
		setRemoteOnSearchAction();
		setOnAddResultsAction();
		setOnNextResultsAction();
		
		initWidget(main);
		
	}
	
	private void setRemoteCorpuses(){

		service.fetchCorpusesAndAttributes(new AsyncCallback<CorpusInformations[]>(){
			public void onSuccess(CorpusInformations[] resultArray){
				ConcordanceWidget.this.searchForm.setCorpusesAndAttributes(resultArray);
			}
			public void onFailure(Throwable t){
				Window.alert(t.getMessage());
			}
		});
		
	}
	
	private void setRemoteOnSearchAction(){

		searchForm.addSearchHandler(new SearchHandler() {
			public void onSearch(SearchEvent event) {
				
				//CollectedSearchParameters search = ConcordanceWidget.this.searchForm.getParameters();
				CollectedSearchParameters search = new CollectedSearchParameters("capku_capku", new CollectedSimpleOrCqlParameters("[lemma=\"leze_pes_pres_oves\"]", true));
				
				ConcordanceWidget.this.service.fetchConcordanceResultsArray(search, new AsyncCallback<ResultTableRow[]>(){
					public void onSuccess(ResultTableRow[] resultArray){
						ConcordanceWidget.this.resultTable.rebuildTable(new ResultsTableSource(resultArray));
					}
					public void onFailure(Throwable t){
						Window.alert(t.getMessage());
					}
				});
			}
		});

	}
	
	private void setOnAddResultsAction(){

		resultTable.getAddResultsButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ConcordanceWidget.this.resultTable.addSource(new ResultsTableSource());
				
				//pri dosazeni konecneho poctu vysledku se odnastavi tlacitko
				//ConcordanceFormWidget.this.resultTable.disableAddResultsButton();
			}
		});

	}

	private void setOnNextResultsAction(){

		resultTable.getNextResultsButton().addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				ConcordanceWidget.this.resultTable.rebuildTable(new ResultsTableSource());
				
				//pri dosazeni konecneho poctu vysledku se odnastavi tlacitko
				//ConcordanceFormWidget.this.resultTable.disableAddResultsButton();
			}
		});
		
	}
	
}
